import { SET_LIST, SET_ROW_LIST, RESET_LIST } from "./type";

export type ListType = {
  id: string;
  startTime: string;
  endTime: string;
  departureAirport: string;
  landingAirport: string;
  price: string;
  realPrice: number;
  flightName: string;
  priceDesc: string;
  tips?: string;
  seconds: number;
};

type OwnState = {
  list: ListType[];
  rowList: ListType[];
};
export function list(
  state: OwnState = { list: [], rowList: [] },
  action: { type: string; data: ListType[] }
) {
  if (action.type === SET_LIST) {
    return {
      ...state,
      list: [...action.data],
    };
  } else if (action.type === SET_ROW_LIST) {
    return {
      list: [...action.data],
      rowList: [...action.data],
    };
  } else if (action.type === RESET_LIST) {
    return {
      ...state,
      list: [...state.rowList],
    };
  } else {
    return state;
  }
}

export function setList(data: ListType[]) {
  return {
    type: SET_LIST,
    data,
  };
}

export function setRowList(data: ListType[]) {
  return {
    type: SET_ROW_LIST,
    data,
  };
}

export function resetList() {
  return {
    type: RESET_LIST,
  };
}
export const getList =
  () =>
  (state: any): ListType[] =>
    state.list.list;
